Could you give me an example of how to attach an object in Deleted state – I keep getting an exception [migrated]

Posted by carewithl on Programmers See other posts from Programmers or by carewithl
Published on 2012-11-12T17:24:49Z Indexed on 2012/11/12 23:13 UTC
Read the original article Hit count: 150

Filed under:

From MSDN :

An object in the Deleted state can only be attached when the ObjectStateManager is already tracking the relationship instance.

Could you give me an example of how to attach an object in the Deleted state. I tried the following and got "System.InvalidOperationException: The object being attached is in an added or deleted state. Relationships cannot be created for objects in this state".

        var contact = context.Contacts.First();
        Console.WriteLine(contact.Addresses.Count()); // 2

        var address = contact.Addresses.First();

        context.Addresses.DeleteObject(address);
        contact.Addresses.Attach(address); // InvalidOperationException

thank you

© Programmers or respective owner

Related posts about entity-framework